﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using Auto.Data.Report;
using CrystalDecisions.CrystalReports.Engine;
using BLL = Auto.Data.BusMan.BLL;



namespace Auto.BusMan.Report
{
    public partial class CarInsuranceListRpt : System.Web.UI.Page
    {
        ReportDocument ReportDoc;

        public string Type
        {
            get
            {
                ViewState["Type"] = Request.QueryString["Type"];
                return (ViewState["Type"] == null) ? "" : ViewState["Type"].ToString();
            }
            set { ViewState["Type"] = value; }
        }

        public string RptName
        {
            get
            {
                return (ViewState["RptName"] == null) ? "" : ViewState["RptName"].ToString();
            }
            set { ViewState["RptName"] = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.CrystalReportViewer1.DisplayGroupTree = false;

            if (!Page.IsPostBack)
            {
                InitPage();
            }
            else
            {
                if (this.Session["CarInsuranceListRpt.ReportDoc"] != null)
                {
                    ReportDoc = (ReportDocument)Session["CarInsuranceListRpt.ReportDoc"];
                    this.CrystalReportViewer1.ReportSource = ReportDoc;//绑定报表控件
                }
            }
        }
        private void InitPage()
        {
            this.ucDDLKeepDiv1.InitControl();
            if (Type == "Insurance") //保险
            {
                Label2.Text = "车辆保险到期时间查询";
            }
            if (Type == "Annual")//年审
            {
                Label2.Text = "车辆年审到期时间查询";

            }
            if (Type == "OperationCertification")//运营证
            {
                Label2.Text = "车辆运营证到期时间查询";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Display();
        }

        private void Display()
        {
            DateTime? EndDate = txtEndDateTextBox.CalendarDate;
            DateTime? InsuranceDate=null;
            DateTime? AnnualVerificationDate=null;
            DateTime? OperationCeriticateDate=null;

            string dateMsg = "";
            if (EndDate == DateTime.MinValue)
            {
                dateMsg = "全部";
            }
            else
            {
                dateMsg = "到日期" + EndDate.ToString() + "止";
            }

            if (Type == "Insurance") //保险
            {
                InsuranceDate = EndDate;
                RptName = "rptCarInsList.rpt";
            }
            if (Type == "Annual")//年审
            {
                AnnualVerificationDate = EndDate;
                RptName = "rptCarAnnualList.rpt";

            }
            if (Type == "OperationCertification")//运营证
            {
                OperationCeriticateDate = EndDate;
                RptName = "rptCarOperationCertificationList.rpt";
            }

            //string strWhere = string.Empty;
            //if (this.ucDDLKeepDiv1.SelectedValue != "")
            //{
            //    strWhere += " AND "+ Model.BusList.FIELD_CompanyID + " ='" +ucDDLKeepDiv1.SelectedValue+"' ";
            //}
            //if (txtEndDateTextBox.Text != "")
            //{
            //    strWhere +=" AND "+ Model.BusList.FIELD_AnnualVerificationDate + "<='" + txtEndDateTextBox.CalendarDateText + "' OR " +
            //               Model.BusList.FIELD_OperationCeriticateDate + "<='" + txtEndDateTextBox.CalendarDateText + "' OR " +
            //               Model.BusList.FIELD_InsuranceDate + "<='" + txtEndDateTextBox.CalendarDateText + "'";
            //}

            DataTable dt = new BLL.BusList().GetListByDateForRpt(ucDDLKeepDiv1.SelectedValue,txtBusIDNO.Text, InsuranceDate, AnnualVerificationDate, OperationCeriticateDate);

            ShowReport(dateMsg, dt);
        }

        //Method
        private void ShowReport(string Msg, DataTable dt)
        {

            if (ReportDoc == null)
            {
                ReportDoc = new ReportDocument();
            }
            ReportDoc.Load(Server.MapPath(RptName));//设置报表文档的路径 
            //设置成 A4纸，其它纸张见 MSDN
            ReportDoc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
            ////设置报表方向为横向
            ReportDoc.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait;

            ReportDoc.SetDataSource(dt);//绑定报表文档的数据源

            SortedList sl = new SortedList();
            ReportManager.FillRptToSortList(sl, "BusMan", ReportDoc, "rptCarInsList.rpt");
            sl.Add("txtTitle", "________公司");
            //sl.Add("DateMsg", Msg);
            //sl.Add("PrintDate", getDateFormatMode(DateTime.Now, true));
            this.CrystalReportViewer1.ReportSource = ReportDoc;//绑定报表控件
            CrystalReportViewer1.ParameterFieldInfo = ReportManager.SeedTextName(sl);// ReportCommand.SeedTextName(sl);
            this.Session["CarInsuranceListRpt.ReportDoc"] = ReportDoc;
        }
    }
}